package menu.view 
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import interfaces.IOperate;
	import menu.MenuC;
	import menu.model.MenuItemVO;
	import morn.core.components.Clip;
	import org.airmvc.Dispatcher;
	import utils.T;
	
	/**
	 * ...
	 * @author WLDragon 2014-03-18
	 */
	public class OrdinaryMenu extends Sprite implements IOperate 
	{
		
		public function OrdinaryMenu() 
		{
			menuItemPool = [];
			for (var i:int = 0; i < 7;i++ )
			{
				menuItemPool.push(new MenuItem(i));
			}
			
			menuItemPanel = new Sprite;
			addChild(menuItemPanel);
			//menuItemPanel.addEventListener(MouseEvent.MOUSE_MOVE,onMouseOver);
			//menuItemPanel.addEventListener(MouseEvent.CLICK,onClick);
			
			arrow = new Clip("png.common.clip_arrow", 1, 2);
			arrow.mouseChildren = false;
			arrow.mouseEnabled = false;
			arrow.y = -16;
			addChild(arrow);
		}
		
		private function onClick(e:MouseEvent):void 
		{
			var m:MenuItem = e.target as MenuItem;
			if (m)
			{
				m.execute();
			}
		}
		
		private function onMouseOver(e:MouseEvent):void 
		{
			var m:MenuItem = e.target as MenuItem;
			if (m)
			{
				index = m.index;
			}
		}
		
		/**
		 * 更新菜单项
		 * @param	type  1-文本 2-图像
		 * @param	items 菜单项数据
		 */
		public function update(type:int,back:int,items:Array,px:int,py:int,col:int):void 
		{
			backType = back;
			curType = type;
			this.x = px;
			this.y = py;
			
			while (menuItemPanel.numChildren > 0)
			{
				menuItemPanel.removeChildAt(0);
			}
			
			var i:int;
			var len:int = items.length;
			for (i = 0; i < len; i++) 
			{
				var d:MenuItemVO = items[i] as MenuItemVO;
				var m:MenuItem = menuItemPool[i] as MenuItem;
				var bmd:BitmapData;
				if (type > 0)
				{
					//位图文本菜单数量等于3,排一列，否则排两列
					bmd = T.font.getPixelWord(d.show);
					if (col != 0)
					{
						column = col;
						m.x = 35 + (i % col) * 140;
						m.y = 10 + (i / col >> 0) * 30;
					}
					else if (len == 2)
					{
						column = 2;
						m.x = 75 + (i % 2) * 140;
						m.y = 25 + (i / 2 >> 0) * 30;
					}
					else if (len > 3)
					{
						column = 2;
						m.x = 35 + (i % 2) * 140;
						m.y = 10 + (i / 2 >> 0) * 30;
					}
					else
					{
						column = 1;
						m.x = 105;
						m.y = 10 + 30 * i;
					}
				}
				else
				{
					//图片菜单排一行
					column = len;
					bmd = App.asset.getBitmapData(d.show);
					m.x = 80 * i;
					m.y = 85 - bmd.height;
				}
					
				m.update(bmd, d.order);
				menuItemPanel.addChild(m);
			}
			
			index = 0;
		}
		
		public function execute(keyCode:int) : void
		{
			switch (keyCode) 
			{
				case Keyboard.A:
					index--;
				break;
				case Keyboard.D:
					index++;
				break;
				case Keyboard.W:
					index -= column;
				break;
				case Keyboard.S:
					index += column;
				break;
				case Keyboard.SPACE:
					curItem.execute();
				break;
				case Keyboard.R:
					//Dispatcher.send(MenuC.CLEAR_PARAMS, this);
					Dispatcher.send(MenuC.EXE_ORDER,this, backType);
				break;
			}
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			if (value < 0 || value >= menuItemPanel.numChildren)
				return;
				
			curItem = menuItemPanel.getChildAt(value) as MenuItem;
			if (curType > 0)
			{
				arrow.frame = 1;
				arrow.x = curItem.x - 16;
				arrow.y = curItem.y + 2;
			}
			else
			{
				arrow.frame = 0;
				arrow.x = curItem.x + curItem.width * 0.5 - 8;
				arrow.y = curItem.y - 12;
			}
			_index = value;
		}
		
		private var _index:int;
		/**箭头*/
		private var arrow:Clip;
		/**当前菜单项*/
		private var curItem:MenuItem;
		/**菜单队列，记录菜单轨迹，用于返回*/
		//private var menuQueue:Vector.<int>;
		/**菜单项池，保存7个MenuItem对象*/
		private var menuItemPool:Array;
		/**菜单项容器*/
		private var menuItemPanel:Sprite;
		/**当前类型，文本或图片*/
		private var curType:int;
		/**返回上一级的类型*/
		private var backType:int;
		/**当前菜单排的列数*/
		private var column:int;
	}

}